package ru.nstu.isma.core.lang.ast.visitor;

import ru.nstu.isma.core.hsm.pde.HMPartialDerEquation;
import ru.nstu.isma.core.lang.ConstValueCalculator;
import ru.nstu.isma.core.lang.ast.LismaBaseVisitor;
import ru.nstu.isma.core.lang.ast.LismaParser;
import ru.nstu.isma.core.hsm.*;
import ru.nstu.isma.core.hsm.exp.HMExpression;
import org.antlr.v4.runtime.misc.NotNull;

import java.util.LinkedList;
import java.util.List;

/**
 * by Alexey Bessonov
 * Date: 03.12.13
 * Time: 0:35
 */
public class CalcConstVisitor extends LismaBaseVisitor<Object> {
    private HMVariableTable table;


    public CalcConstVisitor(HMVariableTable table) {
        this.table = table;
    }

    @Override
    public Object visitConstant(@NotNull LismaParser.ConstantContext ctx) {
        List<HMConst> consts = new LinkedList<HMConst>();
        HMExpression expr = null;
        for (int i = 0; i< ctx.getChildCount(); i++) {
            if (ctx.getChild(i) instanceof LismaParser.Var_identContext) {
                consts.add((HMConst) table.get(ctx.getChild(i).getText()));
            }
        }
        for (HMConst c : consts) {
            c.setValue(ConstValueCalculator.calculate(c));
        }
        return null;
    }

    @Override
    public Object visitInit_cond(@NotNull LismaParser.Init_condContext ctx) {
        VariableVisitor vv = new VariableVisitor();
        String code = vv.visit(ctx.getChild(0)).getCode();
        HMDerivativeEquation equation = (HMDerivativeEquation) table.get(code);
        // TODO временный костыль - не подсчитываем значения у PDE поскольку на данном этапе это не возможно
        if (!(equation instanceof HMPartialDerEquation)){
            HMConst initConst = equation.getInitial();
            initConst.setValue(ConstValueCalculator.calculate(initConst));
        }
        return null;
    }

}
